package com.dlz.systemshop.service.impl;

import com.dlz.systemshop.mapper.GoodsAlbumMapper;
import com.dlz.systemshop.mapper.GoodsMapper;
import com.dlz.systemshop.pojo.Goods;
import com.dlz.systemshop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Integer add(Goods goods) {
        Integer result = goodsMapper.insert(goods);
        return result != 0 ? goods.getId() : -1;
    }

    @Override
    public Boolean update(Goods goods) {
        // 处理 http
        goods.setPicture(dealHttp(goods.getPicture()));
        Integer result = goodsMapper.update(goods);
        return result == 1;
    }

    private String dealHttp(String url) {
        StringBuffer path = new StringBuffer(url);
        int index;
        while ((index = path.indexOf("http")) != -1) {
            int delI = path.indexOf("/",
                    path.indexOf(":",
                            path.indexOf("://", index) + 3
                    ) + 1
            );
            path.delete(index, delI + 1); // 将最后的 / 也删除
        }
        return path.toString();
    }

    @Override
    public Goods getGoods(Integer id) {
        return goodsMapper.queryGoods(id);
    }


    @Override
    public List<Goods> getGoodsListAll() {
        return goodsMapper.getGoodsListAll();
    }

    @Override
    public List<Goods> findGoodsByPage(Integer currentPage, Integer pageSize) {
        return goodsMapper.findGoodsByPage(currentPage, pageSize);
    }

    @Override
    public Integer getCount() {
        return goodsMapper.getCount();
    }

    @Override
    public Boolean delGoods(Integer id) {
        int result = goodsMapper.delGoods(id);
        return result == 1;
    }
}
